package com.pengtu.gsj.controller.base;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.CustomNumberEditor;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;

import com.pengtu.gsj.entity.app.Sysmenu;
import com.pengtu.gsj.entity.app.Trajectory;
import com.pengtu.gsj.entity.app.User;
import com.pengtu.gsj.service.system.SysmenuManagerService;
import com.pengtu.gsj.service.system.TrajectoryService;
import com.pengtu.gsj.utils.UserUtils;
import com.pengtu.gsj.utils.web.SpringMvcUtils;

public class CheckBaseController {

	@Autowired
	private TrajectoryService trajectoryService;
	@Autowired
	private SysmenuManagerService sysmenuManagerService;
	
	@InitBinder
	public void initBinder(ServletRequestDataBinder binder) {
		    binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));    
	        binder.registerCustomEditor(Integer.class, new CustomNumberEditor(Integer.class, true));    
	        binder.registerCustomEditor(Long.class, new CustomNumberEditor(Long.class, true));  
		    binder.registerCustomEditor(Double.class, new CustomNumberEditor(Double.class, true));    
	}
	
	/**
	 * 
	 * loadEntity:
	 * 适用: 每个方法执行之前都会执行该方法
	 * @param model 绑定参数信息
	 * @return 返回持久对象SystemMenu或者新创建对象
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	@ModelAttribute
	public void loadEntity(Model model) throws Exception {
		String left = SpringMvcUtils.getParameter("left");
		model.addAttribute("left", left);
		model.addAttribute("parentId", SpringMvcUtils.getParameter("parentId"));
		
		//绑定轨迹
		//得到当前用户
		User user = UserUtils.getUser();
		StringBuffer href = new StringBuffer();
		if (StringUtils.isNotEmpty(user.getId()) && StringUtils.isNotEmpty(left)) {
			//查询当前用户  当前菜单  在这一天是否已经访问过
			List<Trajectory> trajectories = trajectoryService.getTrajectoriesByUserId(left, user.getId());
			Trajectory trajectory = new Trajectory();
			//若该轨迹已经存在
			if (trajectories.size() > 0) {
				trajectory = trajectories.get(0);
			} else {
				//改轨迹不存在  则添加到数据库
				Sysmenu sysmenu = sysmenuManagerService.get(left);
				trajectory.setSysmenuId(sysmenu.getId());
				trajectory.setSysmenuName(sysmenu.getName());
				trajectory.setUserId(user.getId());
				trajectory.setUserName(user.getName());
				href.append(sysmenu.getHref());
				if (sysmenu.getHref().indexOf("?")>-1) {
					href.append("&");
				} else {
					href.append("?");
				}
				if (sysmenu.getHref().indexOf("left=") == -1) {
					href.append("left=" + SpringMvcUtils.getParameter("left"));
				}
				if (sysmenu.getHref().indexOf("parentId=") == -1) {
					href.append("&parentId=" + SpringMvcUtils.getParameter("parentId"));
				}
				System.out.println("href:"+href.toString());
				trajectory.setOperateHref(href.toString());
			}
			trajectory.setOperateTime(new Date());
			trajectoryService.save(trajectory);
		}
	}
}
